package org.playground.booking.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.playground.booking.dto.BookingDto;
import org.playground.booking.service.IBookingService;
import org.playground.booking.utils.CommonUtils;
import org.playground.booking.utils.PlaygroundBookingException;
import org.playground.booking.utils.UnknownResourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Api
@RestController
@RequestMapping(path = "/api/booking/*")
public class BookingController {

    private static Logger LOGGER = Logger.getLogger(BookingController.class);

    @Autowired
    private IBookingService bookingService;

    @ApiOperation(value = "create booking list",produces = "application/json",consumes = "application/json")
    @RequestMapping(path="/booking",method = RequestMethod.POST,produces = "application/json",consumes = "application/json")
    @ResponseBody
    public ResponseEntity<String> createBooking(@RequestBody BookingDto bookingDto){

        try {
            Long bookingId = bookingService.createBooking(bookingDto);
            return new ResponseEntity<>("{\"bookingid\":\""+bookingId+"\"}",HttpStatus.OK);
        } catch (PlaygroundBookingException e) {

            return new ResponseEntity<>("{\"errorcode\":\""+e.getMessage()+"\"}",HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "update booking status",produces = "application/json",consumes = "application/json")
    @RequestMapping(path="/booking", method = RequestMethod.PUT, produces = "application/json",consumes = "application/json")
    @ResponseBody
    public ResponseEntity<String> updateBooking(@RequestBody BookingDto bookingDto){
        try{
            bookingService.updateBooking(bookingDto);
            return new ResponseEntity<String>("{\"status\":\"success\"}",HttpStatus.OK);
        }catch (PlaygroundBookingException e){
            return new ResponseEntity<String>("{\"errorcode\":\""+e.getMessage()+"\"}",HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(path="/booking", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    @ApiOperation(value = "get the booking list via openid")
    public ResponseEntity<List<BookingDto>> getBookingList(@RequestParam Long userId,
                                                           @RequestParam int pageNb,
                                                           @RequestParam int pageSize,
                                                           @RequestParam boolean includExpired){

        return new ResponseEntity<List<BookingDto>>(bookingService.getBookingList(userId,pageNb,pageSize,includExpired),HttpStatus.FOUND);
    }

    @RequestMapping(path = "/bookings/{userId}/{id}", method = RequestMethod.GET,produces = "application/json")
    public ResponseEntity<List<BookingDto>> getBooking(
            @PathVariable(value="userId") Long userId,
            @PathVariable(value = "id") Long id) throws UnknownResourceException {
        if(id!=null){
            List<BookingDto> bookingDtoList = new ArrayList<BookingDto>();
            BookingDto booking = bookingService.getBooking(userId, id);
            bookingDtoList.add(booking);
            return new ResponseEntity<List<BookingDto>>(bookingDtoList,HttpStatus.OK);
        }else{
            return getBookingList(userId,1,10,true);
        }
    }

    @RequestMapping(path = "booking/{merchantId}/{sportId}/{courtId}/{limit}",method = RequestMethod.GET,produces = "application/json")
    public ResponseEntity<List<BookingDto>> getBookingBySport(
            @PathVariable(value="merchantId") Long merchantId,
            @PathVariable(value="sportId") Long sportId,
            @PathVariable(value="courtId") Long courtId,
            @PathVariable(value="startDate") Date startDate,
            @PathVariable(value = "endDate") Date endDate,
            @PathVariable(value = "limit") Integer limit,
            @PathVariable(value="startFrom") Integer pageIndex
    ){

        List<BookingDto> bookings = bookingService.getBookingList(merchantId,sportId,courtId,startDate,endDate,limit, pageIndex);


        return new ResponseEntity<List<BookingDto>>(bookings,HttpStatus.OK);
    }
}
